home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 November: Tool Chest / Dev.CD Nov 96 TC / Dev.CD Nov 96 TC.toast / Sample Code / Toolbox / ControlStrip Test / ControlStrip Sample.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-07-24  |  9.3 KB  |  344 lines  |  [TEXT/CWIE]

  1. /*************************************************************************
  2. **    Apple Macintosh Developer Technical Support
  3. **
  4. **    Control Strip Sample
  5. **
  6. **    by Matthew Xavier Mora
  7. **  Apple Developer Technical Support 
  8. **  mxmora@apple.com
  9. **
  10. **    File:        ControlStrip Sample.c
  11. **
  12. **    Copyright © 1996 Apple Computer, Inc.
  13. **    All rights reserved.
  14. **
  15. **    You may incorporate this sample code into your applications without
  16. **    restriction, though the sample code has been provided "AS IS" and the
  17. **    responsibility for its operation is 100% yours.  However, what you are
  18. **    not permitted to do is to redistribute the source as "DSC Sample Code"
  19. **    after having made changes. If you're going to re-distribute the source,
  20. **    we require that you make it clear in the source that the code was
  21. **    descended from Apple Sample Code, but that you've made changes.
  22. **
  23. **************************************************************************/
  24.  
  25. /*
  26.     This sample shows how to call the ControlStrip functions
  27. */
  28.  
  29. #include <string.h>
  30.  
  31. // Key definitions
  32. enum {
  33.     returnKeyCode        = 36,
  34.     tabKeyCode            = 48,
  35.     spaceKeyCode        = 49,
  36.     deleteKeyCode        = 51,    
  37.     escapeKeyCode         = 53,
  38.     clearKeyCode        = 71,
  39.     enterKeyCode         = 76,
  40.     f5KeyCode            = 96,
  41.     f6KeyCode            = 97,
  42.     f7KeyCode            = 98,    
  43.     f3KeyCode            = 99,
  44.     f8KeyCode            = 100,
  45.     f9KeyCode            = 101,    
  46.     f11KeyCode            = 103,
  47.     f13KeyCode            = 105,    
  48.     f14KeyCode            = 107,
  49.     f10KeyCode            = 109,
  50.     f12KeyCode            = 111,
  51.     f15KeyCode            = 113,
  52.     helpKeyCode         = 114,
  53.     homeKeyCode            = 115,
  54.     pageUpKeyCode        = 116,
  55.     forwardDelKeyCode    = 117,
  56.     f4KeyCode            = 118,
  57.     endKeyCode            = 119,
  58.     f2KeyCode            = 120,
  59.     pageDownKeyCode        = 121,    
  60.     f1KeyCode            = 122,
  61.     leftArrowKeyCode    = 123,
  62.     rightArrowKeyCode    = 124,
  63.     downArrowKeyCode    = 125,
  64.     upArrowKeyCode        = 126
  65. };
  66.  
  67.  
  68. #define kNumberofSpecialKeys 32
  69. struct KeyTable {
  70.     short keyCode;
  71.     char  name[14];
  72. };
  73.  
  74. typedef struct KeyTable KeyTable;
  75.  
  76.  
  77. KeyTable KeyCodeTable[kNumberofSpecialKeys] = {
  78.     {escapeKeyCode ,"Escape"},
  79.     {f1KeyCode        ,"F1"},
  80.     {f2KeyCode        ,"F2"},
  81.     {f3KeyCode        ,"F3"},
  82.     {f4KeyCode        ,"F4"},
  83.     {f5KeyCode        ,"F5"},
  84.     {f6KeyCode        ,"F6"},
  85.     {f7KeyCode        ,"F7"},
  86.     {f8KeyCode        ,"F8"},
  87.     {f9KeyCode        ,"F9"},
  88.     {f10KeyCode        ,"F10"},
  89.     {f11KeyCode        ,"F11"},
  90.     {f12KeyCode        ,"F12"},
  91.     {f13KeyCode        ,"F13"},
  92.     {f14KeyCode        ,"F14"},
  93.     {f15KeyCode            ,"F15"},
  94.     {upArrowKeyCode        ,"Up Arrow"},
  95.     {downArrowKeyCode    ,"Down Arrow"},
  96.     {leftArrowKeyCode    ,"Left Arrow"},
  97.     {rightArrowKeyCode    ,"Right Arrow"},
  98.     {helpKeyCode         ,"Help"},
  99.     {homeKeyCode        ,"Home"},
  100.     {pageUpKeyCode        ,"Page Up"},
  101.     {forwardDelKeyCode    ,"Forward Delete"},
  102.     {endKeyCode            ,"End"},
  103.     {pageDownKeyCode    ,"Page Down"},
  104.     {deleteKeyCode        ,"Delete"},
  105.     {returnKeyCode        ,"Return"},
  106.     {enterKeyCode         ,"Enter"},
  107.     {clearKeyCode        ,"Clear"},
  108.     {spaceKeyCode        ,"Space"},
  109.     {tabKeyCode            ,"Tab"}
  110. };
  111.  
  112.  
  113. //------------------------------------------------------------------
  114. #pragma mark Includes
  115. //------------------------------------------------------------------
  116. #include     "SimpleApp.h"
  117. #include     "ControlStrip.h"
  118. #include     "Gestalt.h"
  119.  
  120. //------------------------------------------------------------------
  121. #pragma mark Defines
  122. //------------------------------------------------------------------
  123. #define        kDefaultTextSize  9
  124. #define     kMaxSpecs 25        
  125.  
  126. //------------------------------------------------------------------
  127. #pragma mark Globals
  128. //------------------------------------------------------------------
  129.  
  130. Boolean     gHasControlStrip         = false;
  131. Boolean     gCSSupportsUserFont        = false;
  132. Boolean     gCSSupportsUserHotKey    = false;
  133.  
  134. //------------------------------------------------------------------
  135. #pragma mark Prototypes
  136. //------------------------------------------------------------------
  137.  
  138. pascal short DoIdle(EventRecord *evt);
  139. pascal short DoButton(ButtonItemRef me,long m);
  140. pascal short MyUpdate(long m);    
  141.  
  142. #pragma mark -
  143.  
  144. //------------------------------------------------------------------
  145.     pascal short DoButton(ButtonItemRef me,long refcon)
  146. //------------------------------------------------------------------
  147. {
  148. #pragma unused (me,refcon)
  149.  
  150.     Boolean isVisable = SBIsControlStripVisible();
  151.     
  152.     SBShowHideControlStrip(!isVisable);
  153.     
  154.     return noErr;
  155. }
  156.  
  157. //------------------------------------------------------------------
  158. static Boolean HasControlStrip(void)
  159. //------------------------------------------------------------------
  160. {
  161.     short err;
  162.     long response;
  163.     
  164.     gHasControlStrip         = false;     // make sure the globals are set
  165.     gCSSupportsUserFont     = false;
  166.     gCSSupportsUserHotKey     = false;
  167.  
  168.     if (gSAMac.systemVersion >= 0x0700) {
  169.         err  = Gestalt(gestaltControlStripAttr,&response);
  170.         if (err == noErr) {
  171.             gHasControlStrip         = (response & (1 << gestaltControlStripExists));
  172.             gCSSupportsUserFont     = (response & (1 << gestaltControlStripUserFont));
  173.             gCSSupportsUserHotKey     = (response & (1 << gestaltControlStripUserHotKey));
  174.         }
  175.     } 
  176.     
  177.     return gHasControlStrip;
  178. }
  179.  
  180. //------------------------------------------------------------------
  181.     static UInt8 MyLockHandle(Handle h)
  182. //------------------------------------------------------------------
  183. {
  184.     UInt8 handleState;
  185.     
  186.     handleState = HGetState(h);
  187.     HLock(h);
  188.     return handleState;
  189. }
  190.  
  191. //------------------------------------------------------------------
  192. static IsSpecialKey(short KeyCode ,char *name)
  193. //------------------------------------------------------------------
  194. {
  195.     short i;
  196.     
  197.     for (i = 0 ;i < kNumberofSpecialKeys;i++) {
  198.         if (KeyCodeTable[i].keyCode == KeyCode ) {
  199.             strcpy(name,KeyCodeTable[i].name);
  200.             
  201.             return true;
  202.         }
  203.     }
  204.      
  205.     return false;
  206. }
  207.  
  208.  
  209. //------------------------------------------------------------------
  210.     static char GetCharFromKeyCode(UInt16 keyCode)
  211. //------------------------------------------------------------------
  212. {
  213.     static     UInt32     state = 0;                 // should be 0 first time thru
  214.     Handle     keyboardHandle = nil;            // Handle to keyboard resource if needed
  215.     Ptr      kCharCachePtr;                    // Pointer to kCharCache
  216.     char    theChar = 0x00;                    // the char to return
  217. //    UInt8    handleState;
  218.         
  219.     kCharCachePtr = (Ptr)GetScriptManagerVariable(smKCHRCache);
  220.     
  221.     if (kCharCachePtr == nil ) {                                    // should never happen
  222.         long keyboardID = GetScriptManagerVariable(smKeyScript);
  223.  
  224.         keyboardHandle  = GetResource('KCHR',keyboardID);
  225.         
  226.         if (keyboardHandle) {
  227.             //handleState = MyLockHandle(keyboardHandle); // tech note x says we don't have to lock it
  228.             kCharCachePtr = *keyboardHandle;
  229.         }
  230.     }
  231.     
  232.     if (kCharCachePtr) {
  233.         UInt32     result;                            // result from KeyTranslate
  234.  
  235.         result = KeyTranslate(kCharCachePtr,keyCode,&state);        
  236.         theChar = result & charCodeMask;
  237.     }
  238.     
  239.     if (keyboardHandle) {                    // if we got the resource lets release it
  240.         ReleaseResource(keyboardHandle);    // tech note x does this sop we will too.
  241.     }
  242.     
  243.     return theChar;
  244. }
  245.  
  246.  
  247. //------------------------------------------------------------------
  248.     pascal short MyUpdate(long refcon)
  249. //------------------------------------------------------------------
  250. {
  251. #pragma unused (refcon)
  252.  
  253.     Boolean         hotKey;
  254.     unsigned char     keyCode;
  255. //    char             theChar;
  256.     short             modifiers;
  257.     short             err;
  258.     char             name[32];
  259.     
  260.     MoveTo(10,100);
  261.  
  262.     if (gHasControlStrip) {              // if Control strip is installed
  263.         DrawString("\pControl Strip is installed.");
  264.         MoveTo(10,120);
  265.         DrawString("\pClick the button to toggle the state of the control strip.");
  266.         MoveTo(10,140);
  267.         if (gCSSupportsUserHotKey){                    // if we have a version that supports Hot keys
  268.             SBIsShowHideHotKeyEnabled(&hotKey);        // is hotkey enabled?
  269.              
  270.             if (hotKey) {
  271.                 DrawString("\pHot Key is enabled.");
  272.              } else {
  273.                 DrawString("\pHot key is not enabled.");
  274.              }
  275.              MoveTo(10,160);
  276.              
  277.              err = SBGetShowHideHotKey(&modifiers, &keyCode);    // get the hot key combo
  278.              if (err == noErr) {
  279.                  DrawString ("\pHot Key is: ");                    // print the modifiers
  280.                  
  281.                  if (modifiers & cmdKey) {
  282.                      DrawString ("\pCommand + ");
  283.                  }
  284.                  if (modifiers & shiftKey) {
  285.                      DrawString ("\pShift + ");
  286.                  }
  287.                  if (modifiers & optionKey) {
  288.                      DrawString ("\pOption + ");
  289.                  }
  290.                  if (modifiers & controlKey) {
  291.                      DrawString ("\pControl + ");
  292.                  }
  293.                  
  294.                  if (IsSpecialKey(keyCode,name)) {
  295.                     DrawText(name,0,strlen(name));                 
  296.                  } else {
  297.                      char theChar = GetCharFromKeyCode(keyCode);
  298.                      DrawChar(theChar);                                // print the char
  299.                  }
  300.              }
  301.          }
  302.      } else { // if gHasControlStrip
  303.          DrawString("\pControlStrip is not installed.");
  304.      }
  305.      
  306.     return noErr;
  307. }
  308.  
  309.  
  310. //------------------------------------------------------------------
  311.     void main(void)
  312. //------------------------------------------------------------------
  313. {
  314.     short         gMyWindowID;
  315.     short         err;
  316.     Rect         r ;
  317.     short        buttonBottom;
  318.  
  319.     long buttonId = 1;
  320.     
  321.     InitSimpleApp(2,kUseStandardMenu);      // Simple App Sets up the Tool Box For us 
  322.     gMyWindowID = GetDocumentWindow (128);     // Get our stored window 
  323.                                             
  324.  
  325.     SetRectDimensions(&r, 170, 20);            // This sets a rect's size without changing it position
  326.     SetRectLocation(&r, 10, 30);            // This sets a rect's anchor point
  327.  
  328.     SetWindowUpdateProc(gSACurrentWindow,MyUpdate ); //gSACurrentWindow for now, its a Kludge
  329.         
  330.     err = InstallPushButton(&buttonId,gSACurrentWindow,"\pToggle Control Strip",&r,0,DoButton,nil);
  331.     buttonBottom = r.bottom;
  332.     
  333.     if (!HasControlStrip()) {            // No control strip so dim the button
  334.         SADisableMe();                  // disables the current button which is the one we just created
  335.     }
  336.  
  337.     
  338.     GetPrintArea(gSACurrentWindow,&r);    //Set the print area top coordinate
  339.     r.top = buttonBottom + 1 ;          //so that you don't scroll the button out of view
  340.     SetPrintArea(gSACurrentWindow,&r);
  341.     Run();                                 //Let SimpleApp handle the rest
  342. }
  343.  
  344.